from django.utils import timezone
from django.db import models
from account.models import MyUser
#导入编译器定义的字段
from ckeditor_uploader.fields import RichTextUploadingField
# Create your models here.
class BaseModel(models.Model):
    """
    基类，公共字段
    """
    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    update_time = models.DateTimeField('更新时间', auto_now=True)
    is_delete = models.BooleanField('逻辑删除', default=False)

    class Meta:
        # 抽象类，用于继承，迁移时不会创建
        abstract = True
#分类标签
class ArticleTag(BaseModel):
    id= models.AutoField(primary_key=True)
    tag=models.CharField('标签',max_length=500)
    user=models.ForeignKey(MyUser,on_delete=models.CASCADE,verbose_name='用户')
    def __str__(self):
        return self.tag
    class Meta:
        ordering = ['-update_time', '-id']  # 排序
        verbose_name='博客分类'
        verbose_name_plural='博客分类'


#正文内容
class ArticleInfo(BaseModel):
    author=models.ForeignKey(MyUser,on_delete=models.CASCADE,verbose_name='用户')
    title=models.CharField('标签',max_length=200)
    content=RichTextUploadingField('内容')
    articlephoto=models.ImageField('文章图片',blank=True,upload_to='images/article')
    reading=models.IntegerField('阅读量',default=0)
    liking=models.IntegerField('点赞量',default=0)
    #多对多
    article_tag=models.ManyToManyField(ArticleTag,blank=True,verbose_name='文章标签')
    def __str__(self):
        return self.title
    class Meta:
        ordering = ['-update_time', '-id']  # 排序
        verbose_name ='博客管理'
        verbose_name_plural ='博客管理'


#评论信息


class Comment(BaseModel):
    #一对多
    article=models.ForeignKey(ArticleInfo,on_delete=models.CASCADE,verbose_name='所属文章')
    commentator=models.CharField('评论用户',max_length=100)
    content=models.TextField('评论内容')
    created=models.DateTimeField('创建时间',auto_now_add=True)
    def __str__(self):
        return self.article.title
    class Mate:
        ordering = ['-update_time', '-id']  # 排序
        verbose_name ='评论管理'
        verbose_name_plural ='评论管理'